<!DOCTYPE html>
<html lang="en">
<head>
		<title>Snippets &mdash; Boolean Mask Arrays</title>
		<meta charset="utf-8" />
		<link rel="profile" href="http://gmpg.org/xfn/11" />
		<link rel="stylesheet" type="text/css" href="https://corteousquestions.club/theme/css/style.css" />
		<link rel='stylesheet' id='oswald-css'  href='http://fonts.googleapis.com/css?family=Oswald&#038;ver=3.3.2' type='text/css' media='all' />
		<style type="text/css">
			body.custom-background { background-color: #f5f5f5; }
		</style>
		<link rel="alternate" type="application/atom+xml"
			title="Snippets — Flux Atom"
			href="https://corteousquestions.club/" /> 
		<!--[if lte IE 8]><script src="https://corteousquestions.club/theme/js/html5shiv.js"></script><![endif]-->
</head>

<body class="home blog custom-background " >
	<div id="container">
		<div id="header">
				<h1 id="site-title"><a href="https://corteousquestions.club">Snippets</a></h1>
<h2 id="site-description">All that I will forget is here</h2>		</div><!-- /#banner -->
		
		<div id="menu">
			<div class="menu-navigation-container">
				<ul id="menu-navigation" class="menu">
						<li class="active" class="menu-item menu-item-type-post_type menu-item-object-page"><a href="https://corteousquestions.club/category/misc.html">misc</a></li>

				</ul>
			</div> <!--/#menu-navigation-container-->
		</div><!-- /#menu -->
		
		<div class="page-title">
		</div>
	
		<div id="contents">

<div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/boolean-mask.html">Sat 17 April 2021</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/boolean-mask.html" title="Permalink to Boolean Mask Arrays" rel="bookmark">Boolean Mask Arrays</a>
		</h2>
		<div class="entry-content">
			<p>Boolean mask is very useful and handy, when it comes to count, modify,
 extract or manipulate values in an array based on certain condition
 or criteria:</p>
<hr>
<p>arr([1,2,3,4,5,6,7,8,9])</p>
<hr>
<p>Now we create a boolean condition:</p>
<hr>
<p>bool_array = arr &gt; 3
 print(bool_array)
 output: array([False,False,False,True,True,True,True,True,True])</p>
<hr>
<p>Now we create a boolean mask to filter all even numbers in 'arr':</p>
<hr>
<p>mask = 0 == arr % 2
 print(mask)
 output: array([False,True,False,True,False,True,False,True,False])</p>
<hr>
<p>Now with our mask, 'mask', we index on 'arr', that will return a
 1D array with the values that satisfy the condition:</p>
<hr>
<p>even_values = arr[mask]
 print(even_values)
 output: [2,4,6,8]</p>
<hr>
<p>https://bit.ly/2MPWcUb</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/boolean.html" rel="tag">boolean</a>,  <a href="https://corteousquestions.club/tag/mask.html" rel="tag">mask</a>,  <a href="https://corteousquestions.club/tag/numpy.html" rel="tag">numpy</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post-->

		</div>
		
		<div id="footer">
			<p>Powered by <a href="http://getpelican.com">Pelican</a>, theme by <a href="http://bunnyman.info">tBunnyMan</a>.</p>
		</div><!-- /#footer -->
	</div><!-- /#container -->
	<div style="display:none"></div>
</body>
</html>